package com.qdxwx.manage.controller.card;

import com.qdxwx.common.json.AjaxResult;
import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.page.TableDataInfo;
import com.qdxwx.common.utils.TKConvert;
import com.qdxwx.common.utils.TKString;
import com.qdxwx.data.service.card.CardCodeService;
import com.qdxwx.data.service.card.CardCourseService;
import com.qdxwx.data.service.card.CardService;
import com.qdxwx.manage.controller.BaseController;
import com.qdxwx.manage.support.SessionUtils;
import com.qdxwx.models.card.Card;
import com.qdxwx.models.card.CardCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import jakarta.servlet.http.HttpServletRequest;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.List;

/**
 * 课程卡主Controller
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Controller
@RequestMapping("/card/course")
public class CourseCardController extends BaseController {

    @Autowired
    private CardService cardService;
    @Autowired
    private CardCourseService cardCourseService;
    @Autowired
    private CardCodeService cardCodeService;

    @GetMapping("/list.html")
    public String list() {
        return "card/course-card-list";
    }

    /**
     * type ：1课程卡，2充值卡
     */
    @PostMapping("/list.json")
    @ResponseBody
    public TableDataInfo list(HttpServletRequest request) {
        Card card = new Card();
        card.setTypeId(1);
        card.setName(request.getParameter("name"));
        card.setCreateUser(request.getParameter("createUser"));
        card.setStatus(TKConvert.toInteger(request.getParameter("status"), null));
        List<Card> list = cardService.getList(card, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(cardService.count(card));
        return rspData;
    }

    @GetMapping("/add.html")
    public String add() {
        return "card/course-card-add";
    }

    @PostMapping("/add.json")
    @ResponseBody
    public AjaxResult addSave(HttpServletRequest request) {
        String name = request.getParameter("name");
        if (TKString.isEmpty(name)) return error("请输入课程卡名称");
        Integer[] courseIds = TKConvert.toIntArray(request.getParameter("courseIds"));
        if (courseIds.length < 1) return error("请选择课程");
        String courseName = request.getParameter("courseName");
        LocalDate beginTime = TKConvert.toLocalDate(request.getParameter("beginTime"));
        LocalDate endTime = TKConvert.toLocalDate(request.getParameter("endTime"));
        if (beginTime.isAfter(endTime)) return error("到期时间必须大于开始时间");
        int num = TKConvert.toInteger(request.getParameter("num"));
        if (num < 1 || num > 5000) return error("生成数量必须在1-5000张");
        Card card = new Card();
        card.setTypeId(1);//课程卡
        card.setStatus(1);
        card.setName(name);
        card.setRemark(courseName);
        card.setBeginDate(beginTime);
        card.setEndDate(endTime);
        card.setNum(num);
        card.setCreateUser(SessionUtils.getManager().getManagerName());
        int row = cardService.insert(card);
        if (row > 0) {
            cardCodeService.create(card.getId(), num);
            cardCourseService.insertBatch(card.getId(), Arrays.asList(courseIds));
        }
        return toAjax(row);
    }

    @GetMapping("/info.html")
    public String cardInfo(HttpServletRequest request, ModelMap mmap) {
        long cardId = TKConvert.toLong(request.getParameter("cardId"));
        if (cardId < 1) return error404;
        mmap.put("cardId", cardId);
        return "card/course-card-info";
    }

    @PostMapping("/info/list.json")
    @ResponseBody
    public TableDataInfo cardInfoList(HttpServletRequest request) {
        long cardId = TKConvert.toLong(request.getParameter("cardId"));
        if (cardId < 1) return TableDataInfo.error("参数异常，id:value=" + cardId);
        String status = request.getParameter("status");
        String code = request.getParameter("cardCode");
        CardCode cardCode = new CardCode();
        cardCode.setStatus(TKString.isNotEmpty(status) ? status : null);
        cardCode.setCardCode(TKString.isNotEmpty(code) ? code : null);
        cardCode.setCardId(cardId);
        List<CardCode> list = cardCodeService.getList(cardCode, PageRequest.form(request));
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(0);
        rspData.setRows(list);
        rspData.setTotal(cardCodeService.count(cardCode));
        return rspData;
    }

}
